/*******************************************************************************
*  file    : transformwindow.cpp
*  created : 23.06.2014
*  author  : Slyshyk Oleksiy (alexSlyshyk@gmail.com)
*******************************************************************************/

#include "transformwindow.hpp"
#include "ui_transformwindow.h"
#include <QDebug>
#include <QSettings>

TransformWindow::TransformWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TransformWindow)
{
    settingsGroup_ = "transform_window";
    ui->setupUi(this);

    connect(ui->zoneFromSpin, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged(double)) );
    connect(ui->zoneToSpin,   SIGNAL(valueChanged(double)), this, SLOT(onValueChanged(double)) );
}

TransformWindow::~TransformWindow()
{
    delete ui;
}

void TransformWindow::addTransformation(QString s)
{
    ui->comboBox->addItem(s);
}

void TransformWindow::updatePatternList(const QStringList &list)
{
    QStringList l ;
    for(int i = 0; i < ui->patternLeftCombo->count(); ++i)
        {
            l << ui->patternLeftCombo->itemText(i);
        }

    if( l != list)
        {
            while(ui->patternLeftCombo->count())
                ui->patternLeftCombo->removeItem(0);

            ui->patternLeftCombo->addItems(list);
        }

    l.clear();
    for(int i = 0; i < ui->patternRightCombo->count(); ++i)
        {
            l << ui->patternRightCombo->itemText(i);
        }

    if( l != list)
        {
            while(ui->patternRightCombo->count())
                ui->patternRightCombo->removeItem(0);

            ui->patternRightCombo->addItems(list);
        }
}

void TransformWindow::saveConfig(QSettings *s)
{
    QSettings* st = s;
    if( !st )
        st  = new QSettings;

    st->beginGroup(settingsGroup_);
    st->setValue("algo",ui->comboBox->currentText());
    st->setValue("norm",ui->normalizationSpin->value());
    st->setValue("smooth",ui->smoothAlgoCombo->currentText());
    st->setValue("patternLeft",ui->patternLeftCombo->currentText());
    st->setValue("patternRight",ui->patternRightCombo->currentText());
    st->setValue("freq_x",ui->freqSpin->value());
    st->setValue("zone_from",ui->zoneFromSpin->value());
    st->setValue("zone_to", ui->zoneToSpin->value());
    st->setValue("fx_offset",ui->offsetSpinBox->value());
    st->endGroup();

    if( !s )
        st->deleteLater();
}

void TransformWindow::loadConfig(QSettings *s)
{
    QSettings* st = s;
    if( !st )
        st  = new QSettings;

    st->beginGroup(settingsGroup_);
    ui->zoneFromSpin->setValue(st->value("zone_from").toDouble());
    ui->zoneToSpin->setValue(st->value("zone_to").toDouble());
    ui->freqSpin->setValue(st->value("freq_x").toDouble());
    ui->normalizationSpin->setValue(st->value("norm").toDouble());
    ui->offsetSpinBox->setValue(st->value("fx_offset").toDouble());

    int sm_idx = ui->smoothAlgoCombo->findText( st->value("smooth").toString() );
    if(sm_idx > -1)
        ui->smoothAlgoCombo->setCurrentIndex(sm_idx);

    QString algo = st->value("algo").toString();
    int algo_idx = ui->comboBox->findText(algo);
    if(algo_idx > -1)
        ui->comboBox->setCurrentIndex(algo_idx);
    else
        if(ui->comboBox->count() > 1)
            ui->comboBox->setCurrentIndex(1);

    QString pattern = st->value("patternLeft").toString();
    int pattern_idx = ui->patternLeftCombo->findText(pattern);
    if(pattern_idx > -1)
        ui->patternLeftCombo->setCurrentIndex(pattern_idx);
    else
        if(ui->patternLeftCombo->count() > 0)
            ui->patternLeftCombo->setCurrentIndex(0);

    pattern = st->value("patternRight").toString();
    pattern_idx = ui->patternRightCombo->findText(pattern);
    if(pattern_idx > -1)
        ui->patternRightCombo->setCurrentIndex(pattern_idx);
    else
        if(ui->patternRightCombo->count() > 0)
            ui->patternRightCombo->setCurrentIndex(0);

    st->endGroup();


    if( !s )
        st->deleteLater();
}

void TransformWindow::onValueChanged(double d)
{
//    if(ui->zoneFromSpin->value() >= ui->zoneToSpin->value())
//        ui->zoneToSpin->setValue(ui->zoneFromSpin->value() + 0.001);

    emit zoneChanged(ui->zoneFromSpin->value(), ui->zoneToSpin->value());
    Q_UNUSED(d)
}

void TransformWindow::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
        case QEvent::LanguageChange:
            ui->retranslateUi(this);
            break;
        default:
            break;
        }
}
const QString &TransformWindow::settingsGroup() const
{
    return settingsGroup_;
}

void TransformWindow::setSettingsGroup(const QString &settingsGroup)
{
    settingsGroup_ = settingsGroup;
}

QVariantMap TransformWindow::params() const
{
    QVariantMap map;
    map["norm"              ] = ui->normalizationSpin->value();
    map["smooth"            ] = ui->smoothAlgoCombo->currentText();
    map["samplerate"        ] = ui->freqSpin->value();
    map["zone_begin"        ] = ui->zoneFromSpin->value();
    map["zone_end"          ] = ui->zoneToSpin->value();
    map["pattern_left_file" ] = ui->patternLeftCombo->currentText();
    map["pattern_right_file"] = ui->patternRightCombo->currentText();
    map["fx_offset"         ] = ui->offsetSpinBox->value();
    map["transformation"    ] = ui->comboBox->currentText();

    return map;
}


